package com.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.context.UserContextHolder;
import com.common.domain.Pages.PageParams;
import com.common.domain.Pages.PageResult;
import com.common.domain.pojo.Result;
import com.order.domain.dto.CourseBuyDto;
import com.order.domain.dto.OrderEvaluateDto;
import com.order.domain.dto.OrderPageDto;
import com.order.domain.pojo.Order;
import com.order.domain.pojo.OrderDetail;
import com.order.domain.vo.OrderDetailVo;
import com.order.domain.vo.OrderListVo;
import com.order.domain.vo.OrderReviewVo;
import com.order.service.IOrderService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 小杰大人
 * @since 2025-09-29
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Resource
    IOrderService iOrderService;

    // 提交订单
    @PostMapping("/generateOrder")
    public void generateOrder(HttpServletResponse response,  @RequestBody CourseBuyDto courseBuyDto) throws IOException {
        String from = iOrderService.generateOrder(courseBuyDto);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(from);
    }
    // 去支付
    @PostMapping("/to/payment")
    public void toPayment(HttpServletResponse response,@RequestParam("orderId") Long orderId) throws IOException {
        String from = iOrderService.toPayment(orderId);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(from);
    }

    // 订单支付成功
    @PostMapping("/payment/success")
    public Result<String> paySuccess(@RequestParam("orderNo") String orderNo) {
        iOrderService.paySuccess(orderNo);
        return Result.success();
    }

    // 查看订单列表
    @GetMapping("/list")
    public Result<PageResult<OrderListVo>> list(OrderPageDto orderPageDto) {
        return Result.success(iOrderService.pageList(orderPageDto));
    }

    // 获取订单详情
    @GetMapping("/detail/{orderId}")
    public Result<OrderDetailVo> detail(@PathVariable("orderId") Long orderId) {
        return Result.success(iOrderService.detailById(orderId));
    }

    // 获取指定用户的已成功购买的课程id
    @GetMapping("/purchaseds")
    public Result<List<Long>> getPurchasedCourseIds(@RequestParam("userId") Long userId) {
        return Result.success(iOrderService.getPurchasedCourseIds(userId));
    }

    // 取消订单
    @PostMapping("/cancel")
    public Result<String> cancel(@RequestParam("orderId") Long orderId) {
        iOrderService.cancel(orderId,false);
        return Result.success();
    }

    // 退款
    @PostMapping("/refund")
    public Result<String> refund(@RequestParam("orderId") Long orderId,@RequestParam("reason") String reason) {
        iOrderService.refund(orderId,reason);
        return Result.success();
    }


}

